﻿using Modbus.Device;
using S7.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;

namespace PriterApp
{
    public partial class Form1 : Form
    {
        public static float dianzuDown;
        public static float dianzuUp;
        public static float noiseDown;
        public static float noiseUp;

        public string PathName = string.Empty;
        public string DateName = string.Empty;

        int errorFlag = 0;
        int printErrorFlag = 0;


        //int autoFlag = 0;
        int proFlag = 0;

        int dianzuCi = 0;
     
        int printNum = 0;


        string product1 = string.Empty;
        string product2 = string.Empty;
        string Length1 = string.Empty;
        string Length2 = string.Empty;

        float pressF = 1f;
        float shiftF = 1f;


        float pressUpF = 1f;
        float shiftUpF = 1f;

        float changeF = 0f;

       public static IniFile.IniFile _ini;
        public static string plcSmart;
        string printDevice;
       
        public static Form1 mainForm;

      
        //bool controlB = true;
        private SerialPort dianzuPort;
        private string dianzuPortName;

        List<float> pressCheckList = new List<float>();
        List<float> shiftCheckList = new List<float>();

        List<float> shiftReduceList = new List<float>();

        CheckModel rcd;

        public static string StiPass = "admin";

        public Form1()
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;

            this.MaximizeBox = false;

            dgChanxian3.EnableHeadersVisualStyles = false;
            dgChanxian3.AllowUserToAddRows = false;

            dgChanxian3.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold);
            dgChanxian3.DefaultCellStyle.Font = new Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold);

            dgCheck.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 18F, System.Drawing.FontStyle.Bold);
            dgCheck.DefaultCellStyle.Font = new Font("微软雅黑", 18F, System.Drawing.FontStyle.Bold);

            dgPlc.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 9F);//, System.Drawing.FontStyle.Bold
            dgPlc.DefaultCellStyle.Font = new Font("微软雅黑", 9F);//, System.Drawing.FontStyle.Bold

            label2.Image = Properties.Resources.btncheckon1;
            label4.Image = Properties.Resources.btncheckon3;

            autoBt.Image = Properties.Resources.monitor_64;
            setBt.Image = Properties.Resources.gear_64;
            toolStripButton5.Image = Properties.Resources.tick_64;

            mainForm = this ;

            
          

         
            IniPlcData();

            _ini = new IniFile.IniFile(Application.StartupPath + @"\config.ini");
           

          

           

             string value6 = _ini.IniReadValue("ID", "value");
             if (value6 != null && value6 != "")
             {
                 printNum = int.Parse(value6);
             }



           



            PathName = _ini.IniReadValue("PATH", "value");

            DateName = string.Format("{0:yyyyMMdd}", DateTime.Now) + ".txt";



            //double d = GetRandomNumberOther(55, 66);


            GetSerialData();

         


            timer1.Start();
            timer4.Start();

            //OpenExcel();


        }


       
        public PlcDataClass pdc = new PlcDataClass();
        bool CheckControl = true;
        bool NoiseControl = true;
        double low = 0d;
        double high = 0d;
        private void DataHandleThread()
        {
            while (true)
            {
                try
                {
                    bool[] temData = ModbusRtuPlc.ReadCoil(1, 0, 1);
                    pdc.StartCheck = temData[0];

                    bool[] tempBitData = ModbusRtuPlc.ReadCoil(1, 400, 11);
                    pdc.DianzuOK= tempBitData[0];
                    pdc.DianzuNG= tempBitData[1];
                    pdc.NaiyaOK= tempBitData[2];
                    pdc.NaiyaNG= tempBitData[3];
                    pdc.PressOK= tempBitData[4];
                    pdc.PressNG= tempBitData[5];
                    pdc.ShiftOK= tempBitData[6];
                    pdc.ShiftNG= tempBitData[7];
                    //pdc.StartCheck= tempBitData[9];
                    pdc.EndCheck= tempBitData[10];

                    ushort[] tempDianzu = ModbusRtuPlc.ReadHoldingRegister(1, 300, 1);

                    if (tempDianzu[0] > 0)
                    {

                        double dataDianzu = (double)tempDianzu[0] / 10000;

                        pdc.DianzuValue = dataDianzu.ToString() + "Ω";
                    }


                    ushort[] tempPress = ModbusRtuPlc.ReadHoldingRegister(1, 302, 1);

                    if (tempPress[0] > 0)
                    {

                        double dataPress = (double)tempPress[0] / 100;

                        pdc.PressValue = dataPress.ToString("0.0") + "N";
                    }



                    ushort[] tempShift = ModbusRtuPlc.ReadHoldingRegister(1, 304, 1);

                    if (tempShift[0] > 0)
                    {

                        double dataShift = (double)tempShift[0] / 1000;


                        if (dataShift >= 8.4d && dataShift <= 8.49d)
                        {
                            dataShift = dataShift + 0.1;
                        }
                       


                        pdc.ShiftValue = dataShift.ToString("0.0") + "mm";
                    }


                }
                catch { }

                try
                {
                    if (pdc.StartCheck)
                    {
                        if (CheckControl)
                        {
                            CheckControl = false;
                            NoiseControl = true;

                            UpdateCheck();
                        }

                    }
                    else if (pdc.EndCheck)
                    {
                        if (NoiseControl)
                        {
                            NoiseControl = false;

                            low = GetRandomNumberOther(42, 50);
                            high = GetRandomNumberOther(51, 59);


                            if (pdc.DianzuNG || pdc.NaiyaNG || pdc.PressNG || pdc.ShiftNG)
                            {

                                int tempN = 10000 + printNum;
                                string tempStr = tempN.ToString().Substring(1, 4);

                                try { SaveTxt(tempStr, "FAIL", pdc.DianzuValue + "-" + pdc.PressValue + "-" + pdc.ShiftValue + "-" + low + "dB," + high + "dB", DateTime.Now.ToString()); }
                                catch
                                { }


                                printNum++;

                                try
                                { _ini.IniWriteValue("ID", "value", printNum.ToString()); }
                                catch
                                { }
                               
                            }
                            else
                            {
                                int tempN = 10000 + printNum;
                                string tempStr = tempN.ToString().Substring(1, 4);

                                try { SaveTxt(tempStr, "PASS", pdc.DianzuValue + "-" + pdc.PressValue + "-" + pdc.ShiftValue + "-" + low + "dB," + high + "dB", DateTime.Now.ToString()); }
                                catch
                                { }

                                printNum++;

                                try
                                { _ini.IniWriteValue("ID", "value", printNum.ToString()); }
                                catch
                                { }
                            }
                        }
                       

                        for (int i = 0; i < dgCheck.Rows.Count; i++)
                        {
                            if (dgCheck.Rows[i].Cells[0].Value.ToString() == "噪音测试")
                            {
                                dgCheck.Rows[i].Cells[1].Value = low.ToString() + "dB," + high.ToString() + "dB";
                                dgCheck.Rows[i].Cells[2].Value = "OK";

                                dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;

                                //break;
                            }
                            else if (dgCheck.Rows[i].Cells[0].Value.ToString() == "电阻测试")
                            {
                                dgCheck.Rows[i].Cells[1].Value = pdc.DianzuValue;
                                if (pdc.DianzuOK)
                                {
                                    dgCheck.Rows[i].Cells[2].Value = "OK";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;
                                }
                                else if (pdc.DianzuNG)
                                {
                                    dgCheck.Rows[i].Cells[2].Value = "NG";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                                }

                                //break;
                            }
                            else if (dgCheck.Rows[i].Cells[0].Value.ToString() == "耐压测试")
                            {
                                dgCheck.Rows[i].Cells[1].Value = "--";
                                if (pdc.NaiyaOK)
                                {
                                    dgCheck.Rows[i].Cells[2].Value = "OK";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;
                                }
                                else if (pdc.NaiyaNG)
                                {
                                    dgCheck.Rows[i].Cells[2].Value = "NG";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                                }

                                //break;
                            }
                            else if (dgCheck.Rows[i].Cells[0].Value.ToString() == "压力测试")
                            {
                                dgCheck.Rows[i].Cells[1].Value = pdc.PressValue;
                                if (pdc.PressOK)
                                {
                                    dgCheck.Rows[i].Cells[2].Value = "OK";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;
                                }
                                else if (pdc.PressNG)
                                {
                                    dgCheck.Rows[i].Cells[2].Value = "NG";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                                }

                                //break;
                            }
                            else if (dgCheck.Rows[i].Cells[0].Value.ToString() == "行程测试")
                            {
                                dgCheck.Rows[i].Cells[1].Value = pdc.ShiftValue;
                                if (pdc.ShiftOK)
                                {
                                    dgCheck.Rows[i].Cells[2].Value = "OK";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Lime;
                                }
                                else if (pdc.ShiftNG)
                                {
                                    dgCheck.Rows[i].Cells[2].Value = "NG";

                                    dgCheck.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                                }

                                //break;
                            }
                        }



                    }
                    else if (!pdc.StartCheck)
                    {
                        if (!CheckControl)
                        {
                            CheckControl = true;
                        }
                    }
                }
                catch
                { }
               
            }
        }

        /// <summary>
        /// 初始化PLC数据
        /// </summary>
        private void IniPlcData()
        {
            for (int i = 0; i < 13; i++)
            {
                int index = dgPlc.Rows.Add();
                if (i == 0)
                {
                    dgPlc.Rows[index].Cells[0].Value = "开始触发";
                    dgPlc.Rows[index].Cells[1].Value = false;
                }
                else if (i == 1)
                {
                    dgPlc.Rows[index].Cells[0].Value = "完成触发";
                    dgPlc.Rows[index].Cells[1].Value = false;
                }
                else if (i == 2)
                {
                    dgPlc.Rows[index].Cells[0].Value = "电阻OK";
                    dgPlc.Rows[index].Cells[1].Value = false;
                }
                else if (i == 3)
                {
                    dgPlc.Rows[index].Cells[0].Value = "电阻NG";
                    dgPlc.Rows[index].Cells[1].Value = false;
                }
                else if (i == 4)
                {
                    dgPlc.Rows[index].Cells[0].Value = "耐压OK";
                    dgPlc.Rows[index].Cells[1].Value = false;
                }
                else if (i == 5)
                {
                    dgPlc.Rows[index].Cells[0].Value = "耐压NG";
                    dgPlc.Rows[index].Cells[1].Value = false;
                }
                else if (i == 6)
                {
                    dgPlc.Rows[index].Cells[0].Value = "压力OK";
                    dgPlc.Rows[index].Cells[1].Value = false;
                }
                else if (i == 7)
                {
                    dgPlc.Rows[index].Cells[0].Value = "压力NG";
                    dgPlc.Rows[index].Cells[1].Value = false;
                }
                else if (i == 8)
                {
                    dgPlc.Rows[index].Cells[0].Value = "行程OK";
                    dgPlc.Rows[index].Cells[1].Value = false;
                }
                else if (i == 9)
                {
                    dgPlc.Rows[index].Cells[0].Value = "行程NG";
                    dgPlc.Rows[index].Cells[1].Value = false;
                }
                else if (i == 10)
                {
                    dgPlc.Rows[index].Cells[0].Value = "电阻值";
                    dgPlc.Rows[index].Cells[1].Value = 0;
                }
                else if (i == 11)
                {
                    dgPlc.Rows[index].Cells[0].Value = "压力值";
                    dgPlc.Rows[index].Cells[1].Value = 0;
                }
                else if (i == 12)
                {
                    dgPlc.Rows[index].Cells[0].Value = "行程值";
                    dgPlc.Rows[index].Cells[1].Value = 0;
                }
            
            }
        }


        private void GetSerialData()
        {
            //dianliuPortName = _ini.IniReadValue("DianLiuCom", "value");
            dianzuPortName = _ini.IniReadValue("DianZuCom", "value");

            //noisePortName = _ini.IniReadValue("NoiseCom", "value");

            InitSerialPortParameter();
        }

        static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }

        public static double GetRandomNumberOther(double minimum, double maximum)   //Len小数点保留位数
        {

            Random random = new Random(GetRandomSeed());
            return Math.Round(random.NextDouble() * (maximum - minimum) + minimum, 1);
            //Random ran_int = new Random();
            //Random ran_float = new Random();
            //int ints = ran_int.Next((int)minimum, (int)maximum);
            //double b = ran_float.NextDouble();
            //return (ints + b).ToString("F1");
        }

        private void InitSerialPortParameter()
        {
            try
            {
                //dianzuPort = new SerialPort(dianzuPortName, 19200, Parity.Even, 8, StopBits.One);
                //dianzuPort.Open();

                TcpClient tc = new TcpClient("192.168.6.6", 502);
                //tc.Connect("192.168.6.6", 102);


                //ModbusRtuPlc.master = ModbusSerialMaster.CreateRtu(dianzuPort);

                //ModbusIpMaster.CreateIp()
                ModbusRtuPlc.master = ModbusIpMaster.CreateIp(tc);


                toolStripLabel1.Text = "PLC连接成功";
                toolStripLabel1.ForeColor= Color.Lime; 
            }
            catch(Exception ex)
            {
                toolStripLabel1.Text = "PLC连接异常";
                toolStripLabel1.ForeColor = Color.Red;
                MessageBox.Show(ex.ToString());
                return;
            }


            //TcpClient tc = new TcpClient("192.168.6.6", 102);



            ////ModbusRtuPlc.master = ModbusSerialMaster.CreateRtu(dianzuPort);

            ////ModbusIpMaster.CreateIp()
            //ModbusRtuPlc.master = ModbusIpMaster.CreateIp(tc);

            Thread th = new Thread(new ThreadStart(DataHandleThread));
            th.IsBackground = true;
            th.Start();


            //Thread th1 = new Thread(new ThreadStart(ReadTxt));
            //th1.IsBackground = true;
            //th1.Start();
        }





        private void UpdateCheck()
        {
            dgCheck.Rows.Clear();
            for (int i = 0; i < 5; i++)
            {
                int index = dgCheck.Rows.Add();
                if (i == 0)
                {
                    dgCheck.Rows[index].Cells[0].Value = "噪音测试";
                    dgCheck.Rows[index].Cells[1].Value = "--";
                    dgCheck.Rows[index].Cells[2].Value = "--";

                    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else if (i == 1)
                {
                    dgCheck.Rows[index].Cells[0].Value = "电阻测试";
                    dgCheck.Rows[index].Cells[1].Value = "--";
                    dgCheck.Rows[index].Cells[2].Value = "--";

                    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else if (i == 2)
                {
                    dgCheck.Rows[index].Cells[0].Value = "耐压测试";
                    dgCheck.Rows[index].Cells[1].Value = "--";
                    dgCheck.Rows[index].Cells[2].Value = "--";

                    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else if (i == 3)
                {
                    dgCheck.Rows[index].Cells[0].Value = "压力测试";
                    dgCheck.Rows[index].Cells[1].Value = "--";
                    dgCheck.Rows[index].Cells[2].Value = "--";

                    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else if (i == 4)
                {
                    dgCheck.Rows[index].Cells[0].Value = "行程测试";
                    dgCheck.Rows[index].Cells[1].Value = "--";
                    dgCheck.Rows[index].Cells[2].Value = "--";

                    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                }
                //else if (i == 2)
                //{
                //    dgCheck.Rows[index].Cells[0].Value = "加热(中档)-电流";
                //    dgCheck.Rows[index].Cells[1].Value = "2.44A";
                //    dgCheck.Rows[index].Cells[2].Value = "OK";

                //    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
                //}
                //else if (i == 3)
                //{
                //    dgCheck.Rows[index].Cells[0].Value = "加热(低档)-电流";
                //    dgCheck.Rows[index].Cells[1].Value = "0A";
                //    dgCheck.Rows[index].Cells[2].Value = "NG";

                //    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Red;
                //}


            }


            //dgCheck.ClearSelection();

        }


        private void UpdateCheckDianzu()
        {
            dgCheck.Rows.Clear();
           
                int index = dgCheck.Rows.Add();
              
                    dgCheck.Rows[index].Cells[0].Value = "电阻测试";
                    dgCheck.Rows[index].Cells[1].Value = "--";
                    dgCheck.Rows[index].Cells[2].Value = "--";

                    dgCheck.Rows[index].DefaultCellStyle.ForeColor = Color.Lime;
               


            //dgCheck.ClearSelection();

        }

        //private void ThreadMethod()
        //{
        //    try
        //    {
        //        while (true)
        //        {
        //            if (pmc.PlcState.StartCheck1 && !pmc.PlcState.CheckOver1)
        //            {
        //                //toolStripButton4.ForeColor = Color.Black;
        //                printErrorFlag = 0;
        //                errorFlag = 0;
                   
        //                //controlB = true;
        //                //float pressV=0f;
        //                while (true)
        //                {
        //                    if (!pressCheckList.Contains(pmc.PlcState.PressValue)) 
        //                    {
        //                        pressCheckList.Add(pmc.PlcState.PressValue);
        //                    }
        //                    //pressV = pmc.PlcState.PressValue;

        //                    if (pressCheckList.Max() >= pressF && pressCheckList.Max() <= pressUpF) 
        //                    {                             
        //                        break;
        //                    }

        //                    if (!pmc.PlcState.StartCheck1 && !pmc.PlcState.CheckOver1)
        //                    {
        //                        break;
        //                    }
        //                }


        //                if (pressCheckList.Max() >= pressF && pressCheckList.Max() <= pressUpF)
        //                {
        //                    while (true)
        //                    {
        //                        if (pmc.PlcState.StartCheck2 && !pmc.PlcState.CheckOver2)
        //                        {
        //                            //float shiftV = 0f;
        //                            while (true)
        //                            {
        //                                if (!shiftCheckList.Contains(pmc.PlcState.ShiftValue))
        //                                {
        //                                    shiftCheckList.Add(pmc.PlcState.ShiftValue);
        //                                }
        //                                //pressV = pmc.PlcState.PressValue;

        //                                if (shiftCheckList.Max() >= shiftF && shiftCheckList.Max() <= shiftUpF) 
        //                                {
        //                                    //shiftV = shiftCheckList.Max();
        //                                    break;
        //                                }

        //                                if (!pmc.PlcState.StartCheck2 && !pmc.PlcState.CheckOver2)
        //                                {
        //                                    break;
        //                                }
        //                            }


        //                            if (shiftCheckList.Max() < shiftF || shiftCheckList.Max() > shiftUpF)
        //                            {
        //                                textBox3.ForeColor = Color.Red;
        //                                textBox3.Text = "位移不合格";
        //                                textBox2.ForeColor = Color.Red;
        //                                float tempShift = shiftCheckList.Max();
        //                                textBox2.Text = tempShift.ToString("0.0") + "mm";
        //                                textBox1.Text = "";
        //                                //textBox2.Text = "";

        //                                pressCheckList.Clear();
        //                                shiftCheckList.Clear();
        //                                break;
        //                            }
        //                            else
        //                            {
        //                                textBox1.ForeColor = Color.Blue;
        //                                textBox2.ForeColor = Color.Blue;
        //                                textBox3.ForeColor = Color.Blue;

        //                                textBox3.Text = "合格";

        //                                float tempPress = pressCheckList.Max() * changeF;
        //                                float tempShift = shiftCheckList.Max();

        //                                textBox1.Text = tempPress.ToString("0.0") + "N";
        //                                textBox2.Text = tempShift.ToString("0.0") + "mm";



        //                                pressCheckList.Clear();
        //                                shiftCheckList.Clear();

        //                                errorFlag = 1;

        //                                try
        //                                {
        //                                    bcc.CreateQuickMark(pictureBox1, textBox1.Text + "," + textBox2.Text);
        //                                    errorFlag = 2;

        //                                    int tempN = 10000 + printNum;
        //                                    string tempStr = tempN.ToString().Substring(1, 4);

        //                                    Combo(textBox1.Text, textBox2.Text, tempStr);
        //                                    errorFlag = 3;

        //                                    printNum++;

        //                                    _ini.IniWriteValue("ID", "value", printNum.ToString());

        //                                    errorFlag = 4;
        //                                }
        //                                catch
        //                                { errorFlag = 10; }


        //                                // pri
        //                                //if(controlB)
        //                                StartPrintCode(pictureBox2);

        //                                errorFlag = 5;
        //                                //Thread.Sleep(3000);

        //                                //pictureBox1.Image = null;
        //                                //pictureBox2.Image = null;
        //                                //pictureBox3.Image = null;

        //                                break;
        //                            }
        //                        }
        //                        else if (!pmc.PlcState.StartCheck2 && pmc.PlcState.CheckOver2)
        //                        {
        //                            //if (shiftCheckList.Max() >= shiftF)
        //                            {
        //                               // textBox1.ForeColor = Color.Blue;
        //                               // textBox2.ForeColor = Color.Blue;
        //                               // textBox3.ForeColor = Color.Blue;

        //                               // textBox3.Text = "合格";

        //                               // float tempPress = pressCheckList.Max() * changeF;
        //                               // float tempShift = shiftCheckList.Max();

        //                               // textBox1.Text = tempPress.ToString("0.0")+"N";
        //                               // textBox2.Text = tempShift.ToString("0.0") + "mm";

                                       

        //                               // pressCheckList.Clear();
        //                               // shiftCheckList.Clear();

        //                               // errorFlag = 1;

        //                               // try
        //                               // {
        //                               //     bcc.CreateQuickMark(pictureBox1, textBox1.Text + "," + textBox2.Text);
        //                               //     errorFlag = 2;

        //                               //     int tempN = 10000 + printNum;
        //                               //     string tempStr = tempN.ToString().Substring(1, 4);

        //                               //     Combo(textBox1.Text, textBox2.Text, tempStr);
        //                               //     errorFlag = 3;

        //                               //     printNum++;

        //                               //     _ini.IniWriteValue("ID", "value", printNum.ToString());

        //                               //     errorFlag = 4;
        //                               // }
        //                               // catch
        //                               // { errorFlag = 10; }

                                       
        //                               //// pri
        //                               // //if(controlB)
        //                               // StartPrintCode(pictureBox2);

        //                               // errorFlag = 5;
        //                               // //Thread.Sleep(3000);

        //                               // //pictureBox1.Image = null;
        //                               // //pictureBox2.Image = null;
        //                               // //pictureBox3.Image = null;

        //                               // break;
        //                            }
        //                        }
        //                    }
                             
                             
        //                }
        //                else
        //                {
        //                    textBox3.ForeColor = Color.Red;
        //                    textBox3.Text = "压力不合格";
        //                    textBox1.ForeColor = Color.Red;
        //                    float tempPress = pressCheckList.Max() * changeF;
        //                    textBox1.Text = tempPress.ToString("0.0") + "N";
        //                    textBox2.Text = "";

        //                    pressCheckList.Clear();
        //                    shiftCheckList.Clear();
        //                }
        //            }
             

        //            //Thread.Sleep(1000);
        //        }
        //    }
        //    catch
        //    { }
        //}


        //private void ThreadMethod()
        //{
        //    try
        //    {
        //        while (true)
        //        {
        //            if (pmc.PlcState.Product1)
        //            {
        //                if (pmc.PlcState.Product1OkNum > p1NumCompare)
        //                {
        //                    if (pmc.PlcState.CheckOk)
        //                    {
        //                        textBox1.Text = product1;
        //                        textBox1.ForeColor = Color.Blue;
        //                        textBox2.Text = "合格";
        //                        textBox2.ForeColor = Color.Blue;
        //                        textBox3.Text = DateTime.Now.ToString();
        //                        textBox3.ForeColor = Color.Blue;

        //                        p1NumCompare = pmc.PlcState.Product1OkNum;

        //                        try
        //                        {
        //                            StartPrint(product1, "OK",1);
        //                            WriteToExcel(product1, "合格", textBox3.Text);
        //                        }
        //                        catch
        //                        { }

                                
        //                    }
        //                }
        //                else if (pmc.PlcState.Product1ErrorNum > p1NumNonCompare)
        //                {
        //                    if (pmc.PlcState.CheckError)
        //                    {
        //                        textBox1.Text = product1;
        //                        textBox1.ForeColor = Color.Red;
        //                        textBox2.Text = "不合格";
        //                        textBox2.ForeColor = Color.Red;
        //                        textBox3.Text = DateTime.Now.ToString();
        //                        textBox3.ForeColor = Color.Red;

        //                        p1NumNonCompare = pmc.PlcState.Product1ErrorNum;

        //                        try
        //                        {
        //                            WriteToExcel(product1, "不合格", textBox3.Text);
        //                        }
        //                        catch
        //                        { }


        //                    }
        //                }
        //            }
        //            else if (pmc.PlcState.Product2)
        //            {
        //                if (pmc.PlcState.Product2OkNum > p2NumCompare)
        //                {
        //                    if (pmc.PlcState.CheckOk)
        //                    {
        //                        textBox1.Text = product2;
        //                        textBox1.ForeColor = Color.Blue;
        //                        textBox2.Text = "合格";
        //                        textBox2.ForeColor = Color.Blue;
        //                        textBox3.Text = DateTime.Now.ToString();
        //                        textBox3.ForeColor = Color.Blue;

        //                        p2NumCompare = pmc.PlcState.Product2OkNum;

        //                        try
        //                        {
        //                            StartPrint(product2, "OK",2);
        //                            WriteToExcel(product2, "合格", textBox3.Text);
        //                        }
        //                        catch
        //                        { }
        //                    }
                           
        //                }
        //                else if (pmc.PlcState.Product2ErrorNum > p2NumNonCompare)
        //                {
        //                    if (pmc.PlcState.CheckError)
        //                    {
        //                        textBox1.Text = product2;
        //                        textBox1.ForeColor = Color.Red;
        //                        textBox2.Text = "不合格";
        //                        textBox2.ForeColor = Color.Red;
        //                        textBox3.Text = DateTime.Now.ToString();
        //                        textBox3.ForeColor = Color.Red;

        //                        p2NumNonCompare = pmc.PlcState.Product2ErrorNum;

        //                        try
        //                        {
        //                            WriteToExcel(product2, "不合格", textBox3.Text);
        //                        }
        //                        catch
        //                        { }


        //                    }
        //                }
        //            }

        //            Thread.Sleep(1000);
        //        }
        //    }
        //    catch
        //    { }
        //}

        #region 切换功能
        private void label2_Click(object sender, EventArgs e)
        {
           
        }

        private void label4_Click(object sender, EventArgs e)
        {
          
        }
        #endregion

        #region 功能按钮
        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region 定时器
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {

                //toolStripLabel2.Text = pmc.PlcState.Product1OkNum.ToString();
                //toolStripLabel4.Text = pmc.PlcState.Product1ErrorNum.ToString();
                //toolStripLabel6.Text = pmc.PlcState.Product2OkNum.ToString();
                //toolStripLabel8.Text = pmc.PlcState.Product2ErrorNum.ToString();


                //if (dgPlc.Rows.Count > 16)
                //{
                dgPlc.Rows[0].Cells[1].Value = pdc.StartCheck;
                if (pdc.StartCheck)
                {
                    dgPlc.Rows[0].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    dgPlc.Rows[0].DefaultCellStyle.ForeColor = Color.White;


                dgPlc.Rows[1].Cells[1].Value = pdc.EndCheck;
                if (pdc.EndCheck)
                {
                    dgPlc.Rows[1].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    dgPlc.Rows[1].DefaultCellStyle.ForeColor = Color.White;



                dgPlc.Rows[2].Cells[1].Value = pdc.DianzuOK;
                if (pdc.DianzuOK)
                {
                    dgPlc.Rows[2].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    dgPlc.Rows[2].DefaultCellStyle.ForeColor = Color.White;



                dgPlc.Rows[3].Cells[1].Value = pdc.DianzuNG;
                if (pdc.DianzuNG)
                {
                    dgPlc.Rows[3].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    dgPlc.Rows[3].DefaultCellStyle.ForeColor = Color.White;

                dgPlc.Rows[4].Cells[1].Value = pdc.NaiyaOK;
                if (pdc.NaiyaOK)
                {
                    dgPlc.Rows[4].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    dgPlc.Rows[4].DefaultCellStyle.ForeColor = Color.White;


                dgPlc.Rows[5].Cells[1].Value = pdc.NaiyaNG;
                if (pdc.NaiyaNG)
                {
                    dgPlc.Rows[5].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    dgPlc.Rows[5].DefaultCellStyle.ForeColor = Color.White;

                dgPlc.Rows[6].Cells[1].Value = pdc.PressOK;
                if (pdc.PressOK)
                {
                    dgPlc.Rows[6].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    dgPlc.Rows[6].DefaultCellStyle.ForeColor = Color.White;


                dgPlc.Rows[7].Cells[1].Value = pdc.PressNG;
                if (pdc.PressNG)
                {
                    dgPlc.Rows[7].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    dgPlc.Rows[7].DefaultCellStyle.ForeColor = Color.White;


                dgPlc.Rows[8].Cells[1].Value = pdc.ShiftOK;
                if (pdc.ShiftOK)
                {
                    dgPlc.Rows[8].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    dgPlc.Rows[8].DefaultCellStyle.ForeColor = Color.White;


                dgPlc.Rows[9].Cells[1].Value = pdc.ShiftNG;
                if (pdc.ShiftNG)
                {
                    dgPlc.Rows[9].DefaultCellStyle.ForeColor = Color.Lime;
                }
                else
                    dgPlc.Rows[9].DefaultCellStyle.ForeColor = Color.White;




                dgPlc.Rows[10].Cells[1].Value = pdc.DianzuValue;

                dgPlc.Rows[11].Cells[1].Value = pdc.PressValue;

                dgPlc.Rows[12].Cells[1].Value = pdc.ShiftValue;

                //    dgPlc.Rows[33].Cells[1].Value = pmc.PlcState.ShiftCheckTime;




            }
            catch
            { 
            }
        }

      
        #endregion

        #region 计数清零按钮
        private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                
             
            }
            catch
            { }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
               
            }
            catch
            { }
        }


      
        #endregion

        #region Excel处理
        //Microsoft.Office.Interop.Excel.Workbook xBook;
        //Microsoft.Office.Interop.Excel.Worksheet xSheet;
        //Microsoft.Office.Interop.Excel.Application xApp;

        //Microsoft.Office.Interop.Excel.Workbook xBookPass;
        //Microsoft.Office.Interop.Excel.Application xAppPass;

        //public void OpenExcel()
        //{
        //    try
        //    {
        //        OpenFileDialog ofd = new OpenFileDialog();
        //        ofd.Filter = "Excel文件(*.xls,*.xlsx)|*.xls;*.xlsx";
        //        ofd.InitialDirectory = targetPath;
        //        ofd.ValidateNames = true;
        //        ofd.CheckPathExists = true;
        //        ofd.CheckFileExists = true;

        //        string strFileFullPath = targetPath;
        //        xApp = new Microsoft.Office.Interop.Excel.Application();
        //        xApp.Visible = true;
        //        //xApp.UserControl = true;

        //        xBook = xApp.Workbooks.Open(strFileFullPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        //        xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];
        //        if (xSheet == null)
        //        {
        //            return;
        //        }
        //        //this.button3.Enabled = false;
        //    }



        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("Excel文件路径不存在！！！", "错误");

        //        return;
        //    }
        //}

        ///// <summary>
        ///// 写入excel
        ///// </summary>
        ///// <param name="dataStr"></param>
        //public void WriteToExcel(string id,string press, string distance,string date,string dianzu,string noiseMin,string noiseMax,string okNg)
        //{
        //    try
        //    {


        //        int countTemp = 0;

        //        do
        //        {
        //            countTemp++;
        //        } while (((Microsoft.Office.Interop.Excel.Range)xSheet.Cells[countTemp, 1]).Text != "" && ((Microsoft.Office.Interop.Excel.Range)xSheet.Cells[countTemp, 1]).Text != null);
        //        //count = countTemp;
        //        xSheet.Cells[countTemp, 1] = id;
        //        xSheet.Cells[countTemp, 2] = dianzu;
        //        xSheet.Cells[countTemp, 3] = press;
        //        xSheet.Cells[countTemp, 4] = distance;
        //        xSheet.Cells[countTemp, 5] = noiseMin;
        //        xSheet.Cells[countTemp, 6] = noiseMax;
        //        xSheet.Cells[countTemp, 7] = okNg;
        //        xSheet.Cells[countTemp, 8] = date;
        //        //count++;


        //    }
        //    catch
        //    { }

        //}

        //public void CloseExcel()
        //{
        //    try
        //    {
        //        xBook.Save();
        //        xBook.Close(Missing.Value, Missing.Value, Missing.Value);
        //        xApp.Quit();
        //    }
        //    catch
        //    { }

        //}

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //CloseExcel();
        }
        #endregion


        #region 打印机
        private void StartPrint(string proName,string checkResult,int model)
        {

            if (!CheckPrinter(printDevice) || !CheckPrinterInLine(printDevice))
            {
                toolStripLabel10.Text = "打印机未连接";
                toolStripLabel10.ForeColor = Color.Red;
                return;
            }
            //if (!CheckPrinterInLine(printDevice))
            //{
            //    toolStripLabel10.Text = "打印机未连接";
            //    toolStripLabel10.ForeColor = Color.Red;
            //    return;
            //}
            ZebraPrintHelper.PrinterProgrammingLanguage = ProgrammingLanguage.EPL;
            ZebraPrintHelper.PrinterName = printDevice;
            ZebraPrintHelper.PrinterType = DeviceType.DRV;


            //string printData = "N" + "\r\n" +"Q100,20" + "\n\r"
            //    + "A165,40,0,4,1,1,N,\"" + proName + "\"" + "\r\n"
            //    + "A165,100,0,4,1,1,N,\"" + checkResult + "\"" + "\r\n"
            //     + "A55,160,0,3,1,1,N,\"" + textBox3.Text + "\"" + "\r\n"
            //     + "P1" + "\r\n"; ;
            DateTime dt = DateTime.Now;

            //string printData = "N" + "\r\n" + "Q240,024" + "\r\n" + "q100" + "\r\n"
            //     + "A80,24,0,1,1,1,N,\"" + proName + "\"" + "\r\n"
            //     + "A80,54,0,1,1,1,N,\"" + checkResult + "\"" + "\r\n"
            //      + "A8,80,0,1,1,1,N,\"" + textBox3.Text + "\"" + "\r\n"
            //      + "P1" + "\r\n";
            string printData = string.Empty;
            if (model == 1)
            {
                printData = "N" + "\r\n" + "Q120,024" + "\r\n" + "q160" + "\r\n" + "ZB" + "\r\n" //+ "q100" + "\r\n"    + "q200" + "\r\n"          
             + "A35,1,0,1,1,1,N,\"" + proName + "\"" + "\r\n"
             + "A55,27,0,1,1,1,N,\"" + checkResult + "\"" + "\r\n"
              + "A25,53,0,1,1,1,N,\"" + dt.ToShortDateString() + "\"" + "\r\n"
              + "A35,73,0,1,1,1,N,\"" + dt.ToLongTimeString() + "\"" + "\r\n"
              + "P1" + "\r\n";
            }
            else if (model == 2)
            {
                printData = "N" + "\r\n" + "Q120,024" + "\r\n" + "q160" + "\r\n" + "ZB" + "\r\n" //+ "q100" + "\r\n"    + "q200" + "\r\n"          
                            + "A"+Length2+",1,0,1,1,1,N,\"" + proName + "\"" + "\r\n"
                            + "A67,27,0,1,1,1,N,\"" + checkResult + "\"" + "\r\n"
                             + "A25,53,0,1,1,1,N,\"" + dt.ToShortDateString() + "\"" + "\r\n"
                             + "A35,73,0,1,1,1,N,\"" + dt.ToLongTimeString() + "\"" + "\r\n"
                             + "P1" + "\r\n";
            }
          


            ZebraPrintHelper.PrintCommand(printData);

            toolStripLabel10.Text = "打印机已连接";
            toolStripLabel10.ForeColor = Color.Lime;


            Thread.Sleep(200);
        }


        //private void StartPrintCode(PictureBox pb)
        //{
        //    try
        //    {
        //        if (!CheckPrinter(printDevice) || !CheckPrinterInLine(printDevice))
        //        {
        //            toolStripLabel10.Text = "打印机未连接";
        //            toolStripLabel10.ForeColor = Color.Red;
        //            return;
        //        }
        //        //if (!CheckPrinterInLine(printDevice))
        //        //{
        //        //    toolStripLabel10.Text = "打印机未连接";
        //        //    toolStripLabel10.ForeColor = Color.Red;
        //        //    return;
        //        //}
        //        ZebraPrintHelper.PrinterProgrammingLanguage = ProgrammingLanguage.EPL;
        //        ZebraPrintHelper.PrinterName = printDevice;
        //        ZebraPrintHelper.PrinterType = DeviceType.DRV;


        //        printErrorFlag = 1;

        //        //Bitmap img = (Bitmap)pictureBox2.Image;

        //        byte[] imgByte = ImageToByteArray(pb.Image);

        //        ZebraPrintHelper.PrintGraphics(imgByte);

        //        printErrorFlag = 2;

        //        //controlB = false;


        //        //string printData = "N" + "\r\n" +"Q100,20" + "\n\r"
        //        //    + "A165,40,0,4,1,1,N,\"" + proName + "\"" + "\r\n"
        //        //    + "A165,100,0,4,1,1,N,\"" + checkResult + "\"" + "\r\n"
        //        //     + "A55,160,0,3,1,1,N,\"" + textBox3.Text + "\"" + "\r\n"
        //        //     + "P1" + "\r\n"; ;
        //        //DateTime dt = DateTime.Now;

        //        //string printData = "N" + "\r\n" + "Q240,024" + "\r\n" + "q100" + "\r\n"
        //        //     + "A80,24,0,1,1,1,N,\"" + proName + "\"" + "\r\n"
        //        //     + "A80,54,0,1,1,1,N,\"" + checkResult + "\"" + "\r\n"
        //        //      + "A8,80,0,1,1,1,N,\"" + textBox3.Text + "\"" + "\r\n"
        //        //      + "P1" + "\r\n";
        //        //string printData = string.Empty;
        //        //if (model == 1)
        //        //{
        //        //    printData = "N" + "\r\n" + "Q120,024" + "\r\n" + "q160" + "\r\n" + "ZB" + "\r\n" //+ "q100" + "\r\n"    + "q200" + "\r\n"          
        //        // + "A35,1,0,1,1,1,N,\"" + proName + "\"" + "\r\n"
        //        // + "A55,27,0,1,1,1,N,\"" + checkResult + "\"" + "\r\n"
        //        //  + "A25,53,0,1,1,1,N,\"" + dt.ToShortDateString() + "\"" + "\r\n"
        //        //  + "A35,73,0,1,1,1,N,\"" + dt.ToLongTimeString() + "\"" + "\r\n"
        //        //  + "P1" + "\r\n";
        //        //}
        //        //else if (model == 2)
        //        //{
        //        //    printData = "N" + "\r\n" + "Q120,024" + "\r\n" + "q160" + "\r\n" + "ZB" + "\r\n" //+ "q100" + "\r\n"    + "q200" + "\r\n"          
        //        //                + "A" + Length2 + ",1,0,1,1,1,N,\"" + proName + "\"" + "\r\n"
        //        //                + "A67,27,0,1,1,1,N,\"" + checkResult + "\"" + "\r\n"
        //        //                 + "A25,53,0,1,1,1,N,\"" + dt.ToShortDateString() + "\"" + "\r\n"
        //        //                 + "A35,73,0,1,1,1,N,\"" + dt.ToLongTimeString() + "\"" + "\r\n"
        //        //                 + "P1" + "\r\n";
        //        //}



        //        //ZebraPrintHelper.PrintCommand(printData);

        //        toolStripLabel10.Text = "打印机已连接";
        //        toolStripLabel10.ForeColor = Color.Lime;


        //        Thread.Sleep(200);
        //    }
        //    catch(Exception ex)
        //    { printErrorFlag = 10;
        //    toolStripButton4.ForeColor = Color.Red;
        //    MessageBox.Show(ex.ToString());
        //    }
          
        //}



        private void StartTextPrint(string proName, string checkResult,DateTime dt)
        {

            if (!CheckPrinter(printDevice) || !CheckPrinterInLine(printDevice))
            {
                toolStripLabel10.Text = "打印机未连接";
                toolStripLabel10.ForeColor = Color.Red;
                return;
            }
            //if (!CheckPrinterInLine(printDevice))
            //{
            //    toolStripLabel10.Text = "打印机未连接";
            //    toolStripLabel10.ForeColor = Color.Red;
            //    return;
            //}
            ZebraPrintHelper.PrinterProgrammingLanguage = ProgrammingLanguage.EPL;
            ZebraPrintHelper.PrinterName = printDevice;
            ZebraPrintHelper.PrinterType = DeviceType.DRV;
            //string a = dt.Date.Date.ToString().Split(' ')[0];
            //string b = dt.TimeOfDay.ToString().Split('.')[0];

            string printData = "N" + "\r\n" + "Q120,024" + "\r\n"  + "q160" + "\r\n" + "ZB" + "\r\n" //+ "q100" + "\r\n"    + "q200" + "\r\n"          
                + "A67,1,0,1,1,1,N,\"" + proName + "\"" + "\r\n"
                + "A67,27,0,1,1,1,N,\"" + checkResult + "\"" + "\r\n"
                 + "A25,53,0,1,1,1,N,\"" + dt.ToShortDateString() + "\"" + "\r\n"
                 + "A35,73,0,1,1,1,N,\"" + dt.ToLongTimeString() + "\"" + "\r\n"
                 + "P1" + "\r\n";


           


            ZebraPrintHelper.PrintCommand(printData);

            toolStripLabel10.Text = "打印机已连接";
            toolStripLabel10.ForeColor = Color.Lime;


            Thread.Sleep(200);
        }


         public byte[] ImageToByteArray(Image image)
         {
             MemoryStream imageStream = new MemoryStream();
             
             image.Save(imageStream, ImageFormat.Bmp);
             //byte[] imageContent = new Byte[imageStream.Length];
             //imageStream.Position = 0;
             //imageStream.Read(imageContent, 0, (int)imageStream.Length);
             return imageStream.ToArray();
         }


        /// <summary>
        /// 检查打印机是否存在
        /// </summary>
        private bool CheckPrinter(string printername)
        {
            foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                if (printername == sPrint)
                    return true;
            }
            return false;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //if (textBox2.Text == "" || textBox3.Text == "")
            //{
            //    MessageBox.Show("请输入打印数据");
            //    return;
            //}
            //try
            //{

            //    //StartTextPrint("P1", "OK",DateTime.Now);
            //}
            //catch
            //{ MessageBox.Show("打印异常"); };
        
        }

        /// <summary>
        /// 检查打印机是否在线
        /// </summary>
        private bool CheckPrinterInLine(string printername)
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");

            foreach (ManagementObject printer in searcher.Get())
            {
                if (printer["Name"].ToString() == printername && printer["WorkOffline"].ToString().ToLower().Equals("false"))
                    return true;
            }
            return false;
        }
        #endregion

        private void button10_Click(object sender, EventArgs e)
        {
            try
            {

              
            }
            catch
            { }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            try
            {

              
            }
            catch
            { }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {

               
            }
            catch
            { MessageBox.Show("写入异常"); }
           
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {

              
            }
            catch
            { MessageBox.Show("写入异常"); }
        
        }


        private BarCodeClass bcc = new BarCodeClass();
     
        private void button2_Click_1(object sender, EventArgs e)
        {

           
            //byte[] imgByte = ImageToByteArray(pictureBox1.Image);
        }

     

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                //toolStripButton4.ForeColor = Color.Black ;

                //printErrorFlag = 0;
                //errorFlag = 0;


                //bcc.CreateQuickMark(pictureBox1,"11");

                //Combo("1.1N", "2.3mm", "0010", "13Ω", "30dB", "50dB","合格");
              
                ////PictureBox pb = new PictureBox();
                ////pb.Width = int.Parse(textBox7.Text);
                ////pb.Height = int.Parse(textBox8.Text);

                

                ////bcc.CreateQuickMark(pb, textBox6.Text);
              

                //StartPrintCode(pictureBox2);



                //pictureBox1.Image = null;
                //pictureBox2.Image = null;
                //pictureBox3.Image = null;
            }
            catch
            { }
        }

        

        private void pictureBox3_Click(object sender, EventArgs e)
        {
          
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {

           
                //DialogResult dr = MessageBox.Show("是否重置？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                //if (dr == DialogResult.OK)
                //{
                //    try
                //    {
                //        pmc.S7Smart200.Dispose();
                //    }
                //    catch
                //    { }


                //    try
                //    {
                //        pmc.S7Smart200.Open();
                //    }
                //    catch
                //    { }
                //}
         
        
          
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            try
            {

              
            }
            catch
            { MessageBox.Show("写入异常"); }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            try
            {

              
            }
            catch
            { MessageBox.Show("写入异常"); }
        }

        private void button10_Click_1(object sender, EventArgs e)
        {
           
                try
                {

                }
                catch
                { }
         
        }

        private void button11_Click_1(object sender, EventArgs e)
        {
            try
            {
              
            }
            catch
            { }
        }

        private void button9_Click_1(object sender, EventArgs e)
        {
            try
            {
              
            }
            catch
            { }
        }

        private void button8_Click_1(object sender, EventArgs e)
        {
            try
            {
              
            }
            catch
            { }
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            try
            {
              
            }
            catch
            { }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            try
            {
              
            }
            catch
            { }
        }

        private void autoBt_Click(object sender, EventArgs e)
        {
            if (autoBt.Text == "自动")
            {
                autoBt.Image = Properties.Resources.heart_64;
                autoBt.Text = "手动";
                //autoFlag = 1;

            
              

            }
            else
            {
                autoBt.Image = Properties.Resources.monitor_64;
                autoBt.Text = "自动";
                //autoFlag = 0;

             
             
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            try
            {
             
            }
            catch
            { }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            try
            {
              
            }
            catch
            { }
        }

        private void button21_Click(object sender, EventArgs e)
        {
            try
            {
               
            }
            catch
            { }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            try
            {
              
            }
            catch
            { }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            try
            {
             
            }
            catch
            { }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            try
            {
            
            }
            catch
            { }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            try
            {
              
            }
            catch
            { }
        }


        bool HandBool = false;
        Thread thSmartTest;
        private void button13_Click(object sender, EventArgs e)
        {
            try
            {
             
            }
            catch
            { }
        }

        public void ThreadMethod()
        {
            

            //Thread.Sleep(2000);


           
        }

        private void button15_Click(object sender, EventArgs e)
        {
            try
            {
               
            }
            catch
            { }
        }

    


      

      
        void pf_noiseUpEvent(float value)
        {
            try
            {
                noiseUp = value;

                _ini.IniWriteValue("NoiseUp", "value", value.ToString());
                MessageBox.Show("写入成功");
            }
            catch
            { }
            
        }

        void pf_noiseDownEvent(float value)
        {
            try
            {
                noiseDown = value;

                _ini.IniWriteValue("NoiseDown", "value", value.ToString());
                MessageBox.Show("写入成功");
            }
            catch
            { }
           
        }

        void pf_dianzuUpEvent(float value)
        {
            try
            {
                dianzuUp = value;

                _ini.IniWriteValue("DianZuUp", "value", value.ToString());
                MessageBox.Show("写入成功");
            }
            catch
            { }
          
        }

        void pf_dianzuDownEvent(float value)
        {
            try
            {
                dianzuDown = value;

                _ini.IniWriteValue("DianZuDown", "value", value.ToString());
                MessageBox.Show("写入成功");
            }
            catch
            { }
           
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否复位当前检测？？？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dr == DialogResult.OK)
            {
                CheckControl = true;
                NoiseControl = true;
               

                dgCheck.Rows.Clear();

                MessageBox.Show("复位成功");
            }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            //dianzuControl = false;
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            label3.Text = DateTime.Now.ToString();

            label15.Text = CheckControl.ToString()+"-"+ NoiseControl.ToString();

            //label10.Text = p1NumCompare + "-" + p1NumNonCompare + "-" + p2NumCompare + "-" + p2NumNonCompare;
            //label1.Text = pressF + "-" + shiftF + "-" + pressUpF + "-" + shiftUpF;


        }


        private void SaveTxt(string id, string state,string value, string date)
        {
            if (!File.Exists(PathName + DateName))
            {

                FileStream fs1 = new FileStream(PathName + DateName, FileMode.Create, FileAccess.Write);//创建写入文件 
                StreamWriter sw = new StreamWriter(fs1);
                sw.WriteLine(id + ";" + state + ";" + value + ";" + date);//开始写入值
                sw.Close();
                fs1.Close();
            }
            else
            {
                FileStream fs = new FileStream(PathName + DateName, FileMode.Open, FileAccess.Write);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(id + ";" + state + ";" + value + ";" + date);//开始写入值
                sr.Close();
                fs.Close();
            }
        }

    }


    public class DataGridViewIefeel : DataGridView
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                base.OnPaint(e);
            }
            catch
            {
                Invalidate();
            }
        }
    }


}
